using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace RainDrops
{
    public class Game : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D dropTexture;
        Texture2D bgScenery;
        Texture2D bgWindow;
        Texture2D titlescreen;
        Rectangle mainScreen;
        DropManager dropManager;
        Player player;
        SpriteFont consoleFont;
        Sprite scenery;
        enum GameStates { TitleScreen, Playing, PlayerDead, GameOver };
        GameStates gameState = GameStates.Playing;

        // Debugging
        double debugTimeSecs;
        Vector2 debug_sceneryPosition;

        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            this.IsFixedTimeStep = true;
            this.TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 32);
            this.Window.Title = "RainDrops pre-alpha v1";
            this.IsMouseVisible = true;
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            titlescreen = Content.Load<Texture2D>("titlescreen");
            bgScenery = Content.Load<Texture2D>("scenery2");
            bgWindow = Content.Load<Texture2D>("window");
            dropTexture = Content.Load<Texture2D>("drop25");
            consoleFont = Content.Load<SpriteFont>("console");
            mainScreen = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
            dropManager = new DropManager(100, dropTexture, mainScreen, .5f);
            player = new Player(mainScreen, dropManager);

            scenery = new Sprite(new Vector2(0, 0), bgScenery);
            scenery.VelocityMotion(new Vector2(-800, 0), new Vector2(0, 0), new Vector2(10, 0), 1.0f);
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)this.Exit();

            switch (gameState)
            {
                case GameStates.TitleScreen:
                    break;

                case GameStates.Playing:
                    dropManager.Update(gameTime);
                    player.Update(gameTime);
                    debugTimeSecs = gameTime.TotalGameTime.TotalSeconds;
                    scenery.Update(gameTime);
                    debug_sceneryPosition = scenery.CurrentPosition;
                    if (scenery.CurrentPosition.X > 1)
                    {
                        scenery.CurrentPosition = new Vector2(-800, 0);
                        scenery.VelocityMotion(new Vector2(-800, 0), new Vector2(0, 0), new Vector2(1, 0), 2.0f);
                    }
                    break;

                case GameStates.PlayerDead:
                    break;

                case GameStates.GameOver:
                    break;
            }

            
            base.Update(gameTime);
        }

        private void DebugText()
        {
            spriteBatch.DrawString(consoleFont, debugTimeSecs.ToString(), new Vector2(5, 5), Color.Black);
            spriteBatch.DrawString(consoleFont, dropManager.debug_dropsStillOnScreen.ToString(), new Vector2(5, 15), Color.Black);
            spriteBatch.DrawString(consoleFont, dropManager.debug_dropCount.ToString(), new Vector2(5, 25), Color.Black);
            spriteBatch.DrawString(consoleFont, dropManager.debug_collision.ToString(), new Vector2(5, 35), Color.Black);
            spriteBatch.DrawString(consoleFont, dropManager.debug_collisionFlash.ToString(), new Vector2(5, 45), Color.Black);
            spriteBatch.DrawString(consoleFont, dropManager.debug_dropAddedCount.ToString(), new Vector2(5, 55), Color.Black);
            
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            spriteBatch.Begin();
            if (gameState == GameStates.TitleScreen)
            {
                spriteBatch.Draw(titlescreen, new Rectangle(0, 0, this.Window.ClientBounds.Width, this.Window.ClientBounds.Height), Color.White);
            }

            if ((gameState == GameStates.Playing) ||
                (gameState == GameStates.PlayerDead) ||
                (gameState == GameStates.GameOver))
            {
                scenery.Draw(spriteBatch);
                spriteBatch.Draw(bgWindow, new Rectangle(0, 0, this.Window.ClientBounds.Width, this.Window.ClientBounds.Height), Color.White);
                dropManager.Draw(spriteBatch);
                DebugText();
            }

            if ((gameState == GameStates.GameOver))
            {
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
